Skip to content

fix: clarify feature flag auth errors#542

Merged
marandaneto merged 1 commit intoPostHog:mainfrom
marandaneto:fix/feature-flag-auth-error-message
Apr 30, 2026
Merged

fix: clarify feature flag auth errors#542
marandaneto merged 1 commit intoPostHog:mainfrom
marandaneto:fix/feature-flag-auth-error-message

Conversation

@marandaneto
Copy link
Copy Markdown
Member

@marandaneto marandaneto commented Apr 30, 2026

💡 Motivation and Context

Relates to #526.

Local feature flag loading uses both the project API key and the personal/feature-flags API key, but 401 errors always blamed only personal_api_key. This keeps the server-provided error detail and asks users to verify both credentials.

💚 How did you test it?

  • uv run pytest posthog/test/test_client.py::TestClient::test_load_feature_flags_unauthorized posthog/test/test_feature_flags.py::TestLocalEvaluation::test_load_feature_flags_wrong_key -q
  • uv run ruff format --check posthog/client.py posthog/test/test_client.py posthog/test/test_feature_flags.py
  • uv run ruff check posthog/client.py posthog/test/test_client.py posthog/test/test_feature_flags.py

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file
  • Added the release label to the PR

@marandaneto marandaneto marked this pull request as ready for review April 30, 2026 06:02
@marandaneto marandaneto requested a review from a team as a code owner April 30, 2026 06:02
@marandaneto marandaneto requested a review from haacked April 30, 2026 06:02
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 30, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/client.py:1353-1370
**Duplicated message content (OnceAndOnlyOnce)**

The error message body — `"Error loading feature flags: {e.message}. Please verify both your project_api_key and personal_api_key. More information: https://posthog.com/docs/api/overview"` — is constructed twice: once as a `%s` format string for the log call, and again as an f-string for the re-raised `APIError`. Extracting it into a local variable eliminates the duplication and keeps the two outputs in sync automatically.

```suggestion
                detail = (
                    f"Error loading feature flags: {e.message}. "
                    "Please verify both your project_api_key and personal_api_key. "
                    "More information: https://posthog.com/docs/api/overview"
                )
                self.log.error("[FEATURE FLAGS] %s", detail)
                self.feature_flags = []
                self.group_type_mapping = {}
                self.cohorts = {}

                if self.flag_cache:
                    self.flag_cache.clear()

                if self.debug:
                    raise APIError(status=401, message=detail)
```

Reviews (1): Last reviewed commit: "fix: clarify feature flag auth errors" | Re-trigger Greptile

Comment thread posthog/client.py Outdated
Copy link
Copy Markdown

@ioannisj ioannisj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, minor optimization from greptile makes sense

@marandaneto marandaneto force-pushed the fix/feature-flag-auth-error-message branch from 183a31c to a0554d6 Compare April 30, 2026 08:50
@marandaneto marandaneto force-pushed the fix/feature-flag-auth-error-message branch from a0554d6 to 85a961b Compare April 30, 2026 08:50
@marandaneto marandaneto merged commit a1c6640 into PostHog:main Apr 30, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants